package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scene;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ISceneService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.hotel.domain.Hotel;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import cn.wolfcode.luowowo.member.domain.Fans;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.serach.service.IStrategySearchService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class CenterController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITravelService travelService;
    @Reference
    ITravelStatisRedisService travelStatisRedisService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IHotelService hotelService;
    @Reference
    private ISceneService sceneService;
    @RequestMapping("/home")
    @RequireLogin
    public String homepage(Model model, @UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        List<Travel> travels = travelService.listByUserId(userInfo.getId());
        int total = 0;
        for (Travel travel : travels) {
            total++;
            Destination dest =  destinationService.getByUserId(travel.getDest().getId());
            TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(travel.getId());
            model.addAttribute("vo",vo);
            travel.setThumbsupnum(vo.getThumbsupnum());
            model.addAttribute("dest",dest);
            model.addAttribute("total",total);
        }

        //粉丝
        List<Fans> fanses = userInfoService.listByUserId(userInfo.getId());
        model.addAttribute("fanses",fanses);
        model.addAttribute("fansCount",fanses.size());


        //关注
        List<UserInfo> userInfos = userInfoService.listByFansId(3L);
        model.addAttribute("userInfos",userInfos);
        model.addAttribute("count",userInfos.size());


        model.addAttribute("travels",travels);
        return "center/homepage";
    }

    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(Model model, @UserParam UserInfo userInfo,  @ModelAttribute("qo")TravelQuery qo ){
        List<Travel> travels = travelService.listByUserId(userInfo.getId());
        int total = 0;
        int favornum = 0;
        int replynum = 0;
        int viewnum = 0 ;
        for (Travel travel : travels) {
            total++;
            Destination dest =  destinationService.getByUserId(travel.getDest().getId());
            TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(travel.getId());
            travel.setFavornum(vo.getFavornum());
            travel.setReplynum(vo.getReplynum());
            travel.setSharenum(vo.getSharenum());
            travel.setViewnum(vo.getViewnum());

            travel.setThumbsupnum(vo.getThumbsupnum());

            favornum+=travel.getFavornum();
            replynum+=travel.getReplynum();
            viewnum +=travel.getViewnum();

            model.addAttribute("dest",dest);
            model.addAttribute("total",total);
        }
        PageInfo<Travel> pageInfo = travelService.query(qo);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("favornum",favornum);
        model.addAttribute("replynum",replynum);
        model.addAttribute("viewnum",viewnum);
        model.addAttribute("travels",travels);



        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "center/mytravelnotes";
    }
    //点评
    @RequestMapping("/review")
    public String review(Model model,@UserParam UserInfo userInfo){
        //通过用户id获取所有收藏的游记
        int thumbsupnum = 0;
        int replynum = 0;
        List<Scene> scenes = sceneService.selectByUserId(userInfo.getId());
        for (Scene scene : scenes) {
            thumbsupnum+=scene.getThumbsupnum();
            replynum+=scene.getReplyNum();
        }
        model.addAttribute("scenes",scenes);
        model.addAttribute("thumbsupnum",thumbsupnum);
        model.addAttribute("replynum",replynum);
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "center/review";
    }

    //收藏
    @RequestMapping("/travelcollection")
    public String travelcollection(Model model,@UserParam UserInfo userInfo){
        //通过用户id获取所有收藏的目的地
        List<Destination> dests = destinationService.selectDestByUserId(userInfo.getId());
        model.addAttribute("dests",dests);

        //通过用户id获取所有收藏的游记
        List<Travel> travels = travelService.selectTravelByUserId(userInfo.getId());
        model.addAttribute("travels",travels);

        //通过用户id获取所有收藏的攻略
        List<StrategyDetail> strategyDetails = strategyDetailService.selectDetailByUserId(userInfo.getId());
        model.addAttribute("strategyDetails",strategyDetails);

        List<Hotel> hotels = hotelService.selectHotelByUserId(userInfo.getId());
        model.addAttribute("count",hotels.size());
        model.addAttribute("hotels",hotels);


        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "center/travelcollection";
    }
    @RequestMapping("/setting")
    public String setting(Model model,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "center/setting";
    }

    //顶
    @RequestMapping("/thumbup")
    @ResponseBody
    public Object travelThumbup(Long sid, @UserParam UserInfo userInfo,Model model) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        boolean ret = travelStatisRedisService.travelThumbup(sid, userInfo.getId());

        TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(sid);

        return new AjaxResult(ret,"").getData(vo);
    }


    @RequestMapping("/update")
    public String update(Model model, @UserParam UserInfo userInfo, Long uid,UserInfo user){


            //如果两者id相同
            //说明当前登录的用户即被访问用户本人
            if (userInfo.getId() == uid) {
                user.setId(uid);
                userInfoService.update(user);
            }
        UserInfo host = userInfoService.get(uid);
        model.addAttribute("userInfo",host);
        return "center/setting";
    }

    @RequestMapping("/getChangeCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        AjaxResult result = new AjaxResult();
        try {
            userInfoService.sendVerifyCode(phone);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
        return result;
    }


    @RequestMapping("/updateHeadImgUrl")
    @ResponseBody
    public Object updateHeadImgUrl(Model model,@UserParam UserInfo userInfo,String imgValue){
        Long id = userInfo.getId();
        userInfoService.updateHeadImgUrl(imgValue,id);
        return new AjaxResult();
    }
    @RequestMapping("/regetUser")
    public Object regetUser(Model model,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        return "center/setting";
    }

    //密码修改
    @RequestMapping("/password")
    @ResponseBody
    public AjaxResult updatePassword(Long id,String phone,String oldpassword,String newpassword){
        //先判断此账号是否与登陆的账号一致
        UserInfo userInfo = userInfoService.checkPhoneNum(phone,oldpassword);
        if(userInfo == null){
            throw new LogicException("账号或密码错误");
        }else{
            userInfoService.updatePassword(id,newpassword);
        }
        return new AjaxResult();
    }

    //修改邮箱
    @RequestMapping("/editemail")
    @ResponseBody
    public AjaxResult editemail(String email,@UserParam UserInfo userInfo){
        Long id = userInfo.getId();
        userInfoService.updateEmail(id,email);
        return new AjaxResult().getData(email) ;
    }

    @RequestMapping("/savenewphone")
    @ResponseBody
    public AjaxResult savenewphone(String newMobile){
        sendVerifyCode(newMobile);
        return new AjaxResult();
    }

    @RequestMapping("/updatephone")
    public void updatephone(@UserParam UserInfo userInfo, String newphone, HttpServletResponse response){

        if(newphone != null){
            userInfoService.updatePhone(userInfo.getId(),newphone);
        }
        try {
            response.sendRedirect("/login.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //删除游记
    @RequestMapping("/deleteTravel")
    public String deleteTravel(Long id,Model model,@UserParam UserInfo userInfo){
        if(id != null){
            travelService.deleteTravel(id);
        }
        List<Travel> travels = travelService.listByUserId(userInfo.getId());
        int total = 0;
        for (Travel travel : travels) {
            total++;
            Destination dest =  destinationService.getByUserId(travel.getDest().getId());
            TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(travel.getId());
            model.addAttribute("vo",vo);
            travel.setThumbsupnum(vo.getThumbsupnum());
            model.addAttribute("dest",dest);
            model.addAttribute("total",total);
        }
        //粉丝
        List<Fans> fanses = userInfoService.listByUserId(userInfo.getId());
        model.addAttribute("fanses",fanses);
        model.addAttribute("fansCount",fanses.size());


        //关注
        List<UserInfo> userInfos = userInfoService.listByFansId(3L);
        model.addAttribute("userInfos",userInfos);
        model.addAttribute("count",userInfos.size());
        model.addAttribute("userInfo",userInfoService.get(userInfo.getId()));
        model.addAttribute("travels",travels);
        return "center/homepage";
    }

    @RequestMapping("/users")
    @ResponseBody
    public List<UserInfo> users(int state,@UserParam UserInfo userInfo,Model model){
        List<UserInfo> userInfos = userInfoService.state(state);
        model.addAttribute("userInfo",userInfo.getId());
        return userInfos;
    }

}
